import wx
import matplotlib
import random

# uncomment the following to use wx rather than wxagg
#matplotlib.use('WX')
#from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas

# comment out the following to use wx rather than wxagg
#matplotlib.use('WXAgg')
#from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

#from matplotlib.backends.backend_wx import NavigationToolbar2Wx

#from matplotlib.figure import Figure

import networkx
import pylab

class MyFrame(wx.Frame):
    def __init__(self, parent=None, id=-1):
        wx.Frame.__init__(self, parent, id, size=(800, 600))
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.panel1 = wx.Panel(self)
        self.panel2 = GraphPanel(self)
        sizer.Add(self.panel1, 1, wx.EXPAND)
        sizer.Add(self.panel2, 1, wx.EXPAND)

        self.SetSizer(sizer)

        box = wx.BoxSizer(wx.VERTICAL)
        nb = wx.Notebook(self.panel1)
        panel = wx.Panel(nb)
        button = wx.Button(panel, -1, 'test', size=(200,100))
        self.Bind(wx.EVT_BUTTON, self.OnClick, button)
        nb.AddPage(panel, 'test')
        box.Add(nb, 1, wx.EXPAND)
        self.panel1.SetSizer(box)

    def OnClick(self, e):
        self.panel2.draw()

class GraphPanel(wx.Panel):
    def __init__(self, parent=None, id=-1):
        wx.Panel.__init__(self, parent, id)
        self.pre_draw()
        self.figure = Figure()
        self.which = -1
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.EXPAND)
        self.SetSizer(self.sizer)
        #self.draw()
        #self.Update()


    def pre_draw(self):
        nodes = ['a','b','c']
        edges = [('a','b'),('b','c'),('a','c')]
        self.G1 = networkx.Graph()
        self.G1.add_nodes_from(nodes)
        self.G1.add_edges_from(edges)

        nodes = ['a','b','c','e']
        edges = [('a','b'),('b','c'),('a','c'),('a','e')]
        self.G2 = networkx.Graph()
        self.G2.add_nodes_from(nodes)
        self.G2.add_edges_from(edges)

    def draw(self):
        self.which *= -1
        self.axes = self.figure.add_subplot(111)
        #networkx.draw(self.G1, ax=self.axes)
        if self.which == 1:
            self.axes.clear()
            self.axes = self.figure.add_subplot(111)
            # draw nodes, coloring by rtt ping time
            networkx.draw(self.G1,
                          with_labels=False,
                          alpha=0.5,
                          node_size=15,ax=self.axes,hold=True)
            #networkx.draw(self.G1, ax = self.axes)
            self.canvas.draw()
            self.Update()
        else:
            self.axes.clear()
            self.axes = self.figure.add_subplot(111)
            networkx.draw(self.G2, ax = self.axes)
            self.canvas.draw()
            self.Update()


class App(wx.App):
    def OnInit(self):
        #self.task = Task()
        self.frame = MyFrame()
        self.frame.Show()
        self.SetTopWindow(self.frame)
        return True



def random_graph(n, e):
    G = networkx.Graph()
    for i in xrange(1, n):
        G.add_node(str(i))

    for i in xrange(1, e):
        node1 = int(random.uniform(1, n))
        node2 = int(random.uniform(1, n))

        G.add_edge(str(node1),str(node2))

    return G

def test_draw(G):
    pos = networkx.graphviz_layout(G)
    #pos = networkx.layout.(G)
    #ax = pylab.axes(100,100)
    pylab.figure(1,figsize=(8,8) ,dpi=75)

    pos2 = {}

    for k,v in pos.items():
        x,y = v
        x = x/1000
        y = y/1000

        pos[k] = (x,y)


    networkx.draw(G, pos, node_size=10)
    pylab.show()

if __name__ == '__main__':
    #app = App()
    #app.MainLoop()

    G = random_graph(100, 200)
    test_draw(G)
'''

import os
import networkx
import pylab

os.chdir('../tmp')
G = networkx.XGraph()
for line in open('pp.result'):
    tmp = line.strip().split('\t')
    G.add_nodes_from([tmp[0],tmp[1]])
    G.add_edge(tmp[0],tmp[1],float(tmp[2]))

networkx.draw(G)
pylab.show()
'''








#END